<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

// @TODO: Get results only where they are set to active

class Category_model extends CI_Model
{
	protected $_table;	    
	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function __construct()
	{
		parent::__construct();
		$this->load->helper('date');
	}
	
	// START CATEGORY MODEL
	
	// get list all category
	function get_list_all_category($limit, $start)
	{
		$this->db->select('id, code, name, description, modified_date, status');
		$this->db->from('vt_danh_muc');
		$this->db->limit($limit, $start);
		$this->db->order_by('name', 'asc');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	// count all category
	function count_all_category($status = '')
	{
		$this->db->select('id');
		$this->db->from('vt_danh_muc');
		if($status != "")
		{
			$this->db->where('status',$status);
		}
		return $this->db->count_all_results();
	}
	
	// add category
	function add_category()
	{
		$fields = array(
				'name'			=> $this->input->post('name'),
				'code'			=> mb_strtoupper($this->input->post('code')),
				'description'	=> $this->input->post('description'),
				'status'		=> 0,
		);
	
		$this->db->set($fields);
		$this->db->insert('vt_danh_muc');
	}
	
	// Edit category
	public function edit_category($id)
	{
		$fields = array(
				'name'			=> $this->input->post('name'),
				'code'			=> mb_strtoupper($this->input->post('code')),
				'description'			=> mb_strtoupper($this->input->post('description'))
		);
	
		$this->db->set($fields);
		$this->db->where('id', $id);
		$this->db->update('vt_danh_muc');
	}
	
	// get category
	function get_category($id)
	{
		$this->db->select('*');
		$this->db->from('vt_danh_muc');
		$this->db->where('id',$id);
		$query = $this->db->get();
		return $query->row();
	}
	public function enablecate($id)
	{
		$fields = array(
				'status'		=> 1
		
		);
		
		$this->db->set($fields);
		$this->db->where('id', $id);
		$this->db->update('vt_danh_muc');
	}
	
	public function check_code_category()
	{
		$code = $this->input->post('code');
		$query = "
		SELECT code
		FROM vt_danh_muc WHERE code ='$code'
		";
		$result = $this->db->query($query);
		if($result->num_rows() >= 1)
		{
		return true;
		}
			else
			{
			return false;
		}
	}
	
	public function category_exchange($id, $status = 0)
	{
		$data = array(
				'status' => $status,
		);
	
		$this->db->where('id', $id);
		$this->db->update('vt_danh_muc', $data);
		if($this->db->affected_rows() >= 0)
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
	
	// END CATEGORY CONTROLLER
}

/* Location: ./application/models/danhmuc_model.php */